package cn.iocoder.yudao.module.member.controller.admin.signin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.*;
import cn.iocoder.yudao.module.member.convert.signin.SignInConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.SignInDO;
import cn.iocoder.yudao.module.member.service.signin.SignInService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 用户签到")
@RestController
@RequestMapping("/member/sign-in")
@Validated
public class SignInController {

    @Resource
    private SignInService signInService;

    @PostMapping("/create")
    @Operation(summary = "创建用户签到")
    @PreAuthorize("@ss.hasPermission('member:sign-in:create')")
    public CommonResult<Long> createSignIn(@Valid @RequestBody SignInCreateReqVO createReqVO) {
        return success(signInService.createSignIn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户签到")
    @PreAuthorize("@ss.hasPermission('member:sign-in:update')")
    public CommonResult<Boolean> updateSignIn(@Valid @RequestBody SignInUpdateReqVO updateReqVO) {
        signInService.updateSignIn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户签到")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:sign-in:delete')")
    public CommonResult<Boolean> deleteSignIn(@RequestParam("id") Long id) {
        signInService.deleteSignIn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户签到")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:sign-in:query')")
    public CommonResult<SignInRespVO> getSignIn(@RequestParam("id") Long id) {
        SignInDO signIn = signInService.getSignIn(id);
        return success(SignInConvert.INSTANCE.convert(signIn));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户签到列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:sign-in:query')")
    public CommonResult<List<SignInRespVO>> getSignInList(@RequestParam("ids") Collection<Long> ids) {
        List<SignInDO> list = signInService.getSignInList(ids);
        return success(SignInConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户签到分页")
    @PreAuthorize("@ss.hasPermission('member:sign-in:query')")
    public CommonResult<PageResult<SignInRespVO>> getSignInPage(@Valid SignInPageReqVO pageVO) {
        PageResult<SignInDO> pageResult = signInService.getSignInPage(pageVO);
        return success(SignInConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户签到 Excel")
    @PreAuthorize("@ss.hasPermission('member:sign-in:export')")
    @OperateLog(type = EXPORT)
    public void exportSignInExcel(@Valid SignInExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SignInDO> list = signInService.getSignInList(exportReqVO);
        // 导出 Excel
        List<SignInExcelVO> datas = SignInConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户签到.xls", "数据", SignInExcelVO.class, datas);
    }

}
